<?php namespace Admin\Controller;
	use Common\Model\Goods;
	use Hdphp\Controller\Controller;
	class GoodsController extends Controller{
	private $db;
	public function __init(){
		$this->db=new Goods;
		parent::__init();
	}
		public function index(){
//		获取数据
		$data=$this->db->orderBy('time','DESC')->get();
		View::with('data',$data); 
           View::make();
		}
		
		public function add(){
			if(IS_POST){				
//				p($_POST);die;
			 $result=$this->db->store();
			if(!$result){
				View::error($this->db->getError());
			}
			View::success('添加成功',U('index'));

			}

//			获取分类数据
			$cateData=Db::table('shop_category')->get();
			$cateData=Data::tree($cateData,'cname');
//			p($cateData);die;
			View::with('cateData',$cateData);
//			
////		分配品牌		
			$brandData=Db::table('shop_brand')->get();
			View::with('brandData',$brandData);
			View::make();
		}
		
		
		public function getTypeAttr(){
			
//			var_dump($_POST['tid']);die;
			$tid=Q('post.tid',0,'intval');
			$_SESSION['tid']=$tid;
//			查询对应的类型属性
			$data=Db::table('shop_type_attr')->where('tid',$tid)->get();
//			p($data);die;
			foreach($data as $k=>$v){
				$data[$k]['tavalue']=explode('|', $v['tavalue']);
			}
//			p($data);die;
//			数据返回到js
			View::ajax($data);die;
		}

		public function upload(){
			$file = Upload::path('Upload/Content/' . date('y/m'))->make();
		    if (empty($file)) {
		        $this->ajax(Upload::getError());
		    } else {
		        /** $file内部就是以下这个数组
		            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
		        );**/
		        //上传成功，把上传好的信息返给js
		        $data = $file[0];
	        echo json_encode($data);exit;
//				View::ajax($data);die;
		    }
		}
      public function edit(){
      	if(IS_POST){
//    		p($_POST);die;
			$result=$this->db->edit();
		if(!$result){
			View::error($this->db->getError());
			}
			View::success('编辑成功',U('index'));
		}
      	$gid=Q('get.gid',0,'intval');
//		获取分类数据
		$cateData=Db::table('shop_category')->get();
		$cateData=Data::tree($cateData,'cname');
        View::with('cateData',$cateData);

//		分配品牌		
		$brandData=Db::table('shop_brand')->get();
		View::with('brandData',$brandData);
//		获取商品数据
		$oldData=Db::table('shop_goods gs')
		->join('goods_detail d','gs.gid','=','d.gid')
		->where('gs.gid',$gid)
		->first();
		$oldData['big']=explode(',', $oldData['big']);
		View::with('oldData',$oldData);
//		查询tid
		$tid=Db::table('shop_goods')->where('gid',$gid)->pluck('tid');		
//		获取所有属性
		$attrData=Db::table('shop_type_attr')->where('tid',$tid)->where('class',0)->get();
		foreach($attrData as $k=>$v){
				$attrData[$k]['tavalue']=explode('|', $v['tavalue']);
		}
//		p($attrData);die;
		View::with('attrData',$attrData);
//		获取当前商品已经选中的属性数据
		$oldAttr=Db::table('goods_attr ga')
		->join('shop_type_attr ta','ga.taid','=','ta.taid')
		->where('gid',$gid)
		->where('class',0)
		->lists('gtvalue');
//		p($oldAttr);die;
		View::with('oldAttr',$oldAttr);
//			处理商品规格,,当前已经选中的商品规格
//			注意有选择一个规格，就有一个selected
		$oldSpec=Db::table('goods_attr ga')
		->join('shop_type_attr ta','ga.taid','=','ta.taid')
		->where('gid',$gid)
		->where('class',1)
		->get();
			
		foreach($oldSpec as $k=>$v){
			$oldSpec[$k]['tavalue']=explode('|', $v['tavalue']);
		}
		View::with('oldSpec',$oldSpec);
		View::make();
      }
    public function del(){
//  	echo 2;die;
		$gid=Q('get.gid',0,'intval');
		$tid=Q('get.tid',0,'intval');
		$this->db->where('gid',$gid)->delete();
		$deModel=new \Common\Model\Goods_detail;
		$deModel->where('gid',$gid)->delete();
		$gtModel=new \Common\Model\Goods_attr;
		$gtModel->where('gid',$gid)->delete();
		View::success('删除成功',U('index'));
    }
	
	}
	
	
	?>